package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.xyz.User;
import com.xyz.UserType;

import jdbc.ConnectionManager;

public class UsersDao {
	
	/*from AbstractDao*/
	//------
	protected Connection getConnection(){
		return ConnectionManager.getInstance().currentConnection();
	}
	
	protected void close(AutoCloseable... closeables) {
        for(AutoCloseable ac : closeables) {
            close(ac);
        }
    }

    protected void close(AutoCloseable c) {
        if(c == null) return ;
        try {
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //-------
    
    
    public UsersDao(){}
    
    public void create(User u){
    	Connection connection=getConnection();
    	String sql="INSERT INTO users (login, pass, userType) VALUES (?, ?, ?)";
    	
    	try(PreparedStatement createSt = connection.prepareStatement(sql)){
    		createSt.setString(1, u.getLogin());
            createSt.setString(2, u.getPass());
            createSt.setString(3, u.getUserType().toString());
            createSt.executeUpdate();
    	}
    	catch(SQLException e){
    		throw new RuntimeException(e);
    	}
    }
    
    public User getByLogin(String login){
    	String sql = "SELECT * FROM users WHERE login=?";
    	Connection connection=getConnection();
    	
    	try(PreparedStatement get = connection.prepareStatement(sql)){
    		get.setString(1, login);
    		
    		ResultSet results = get.executeQuery();
    		
    		if (!results.next())
    			return null;
    		
    		return convert(results);
    	}
    	catch(SQLException e){
    		throw new RuntimeException(e);
    	}
    }
    
    public List<User> getAll(int offset, int limit){
    	String sql="SELECT * FROM users ORDER BY created DESC LIMIT ? OFFSET ?";
    	Connection connection=getConnection();
    	
    	try(PreparedStatement get = connection.prepareStatement(sql)){
    		get.setInt(1, limit);
    		get.setInt(2, offset);
    		
    		ResultSet results=get.executeQuery();
    		
    		List<User> users=new ArrayList<>();
    		
    		while (results.next()){
    			users.add(convert(results));
    		}
    		
    		return users;
    	}
    	catch(SQLException e){
    		throw new RuntimeException(e);
    	}
    	
    }
    
    public List<User> getUsersAll(){
    	String sql="SELECT * FROM users";
    	Connection connection=getConnection();
    	
    	try(PreparedStatement get = connection.prepareStatement(sql)){
    		
    		ResultSet results=get.executeQuery();
    		
    		List<User> users=new ArrayList<>();
    		
    		while (results.next()){
    			users.add(convert(results));
    		}
    		
    		return users;
    	}
    	catch(SQLException e){
    		throw new RuntimeException(e);
    	}
    }
    
    private User convert(ResultSet rs) throws SQLException{
    	String login=rs.getString("login");
    	String pass=rs.getString("pass");
    	String userType=rs.getString("userType");
    	UserType uT;
    	if (userType.equals("admin"))
    		uT=UserType.admin;
    	else if (userType.equals("manager"))
    		uT=UserType.manager;
    	else if (userType.equals("callCenter"))
    		uT=UserType.callCenter;
    	else
    		uT=UserType.freeUser;
    	
    	return new User(login, pass, uT);
    }
    
    
}
